//使用与autojs7以上
var serverurl = "ws://127.0.0.1:8180";
let ws = $web.newWebSocket(serverurl, {
    eventThread: 'this'
});

ws.on("open", (res, ws) => {
    log("WebSocket已打开");
}).on("failure", (err, res, ws) => {
    log("WebSocket连接失败或中断");
    console.error(err);
}).on("closing", (code, reason, ws) => {
    log("WebSocket关闭中");
}).on("closed", (code, reason, ws) => {
    log("WebSocket已关闭: code = %d, reason = %s", code, reason);
});

ws.on("text", (text, ws) => {
    console.info("接收到文本消息: ", text);
    ws.send("client==>"+text);
});

//console.log(ws.send('Hello, Auto.js Pro')); // 返回true

// 脚本退出时取消WebSocket
events.on('exit', () => {
    ws.cancel();
});